import { def } from "./def.js"
import observe from "./observe.js"

export const arrayMethods = Object.create(Array.prototype)
const methodsNeedChange = [
    'push',
    'pop',
    'shift',
    'unshift',
    'splice',
    'sort',
    'reverse'
]
methodsNeedChange.forEach(method => {
    const arrayProto = Array.prototype[method]
    def(arrayMethods, method, function() {
        const res = arrayProto.apply(this, arguments)
        let inserted
        switch (method) {
            case 'push':
            case 'unshift':
                inserted = arguments
                break
            case 'splice':
                inserted = arguments.slice(2)
                break
        }
        if (inserted) {
            const ob = this.__ob__
            ob.observeArray(inserted)
        }
        return res
    })
})